﻿using Abp.BackgroundJobs;
using Abp.Dependency;
using System;
using System.Collections.Generic;
using System.Text;
using Abp.Domain.Repositories;
using FwProject.Core.SettlementInfoModel;
using System.Linq;
using FwProject.Core.Distribution;
using Abp.Domain.Uow;
using Castle.Core.Logging;
using Abp.Threading.BackgroundWorkers;
using Abp.Threading.Timers;

namespace FwProject.BackGround.DistributionEnter
{
    public class DistributionEnterWorkBackGround : PeriodicBackgroundWorkerBase, ISingletonDependency
    {
        private readonly IRepository<DistributionInfo, Guid> _repositoryDistributionInfo;
        public ILogger logger { get; set; }
        public DistributionEnterWorkBackGround(AbpTimer timer, IRepository<DistributionInfo, Guid> repositoryDistributionInfo):base(timer) {

#if DEBUG
            Timer.Period = 1000 * 60;
#else
           Timer.Period = 1000 * 60 * 60 * 24;
#endif
            _repositoryDistributionInfo = repositoryDistributionInfo;
            logger = NullLogger.Instance;
        }
        [UnitOfWork]
        protected override void DoWork()
        {
            try
            {
                using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MayHaveTenant))
                {
                    logger.Info("后台任务开始处理配送确认");
                    var t = _repositoryDistributionInfo.GetAll().Where(o => o.Pricestatus == Pricestatus.已配送 && o.CreationTime.AddDays(7) <= DateTime.Now);
                    logger.Info($"后台任务开始处理配送确认{t.Count()}需要处理");
                    foreach (var item in t)
                    {
                        item.Pricestatus = Pricestatus.未结账;
                    }
                    logger.Info($"后台任务开始处理配送处理完成");

                    CurrentUnitOfWork.SaveChanges();
                }
            }
            catch (Exception exe)
            {

                logger.Error("后台任务开始处理配送处理失败", exe);
            }
           
        }
    }
}
